package test.adddata;

import cn.hutool.core.io.FileUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.ds.druid.DruidDSFactory;
import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import utils.SmallTool;

import javax.sql.DataSource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author: gcr
 * @Date: 2022-3-31 8:01
 */
public class 查找泰医和本地差距进行补充 {

    public static String taiyiTable = "spider_pubmed_vip_extend";
    static String dbName = "pubmed_vip_extend_0328_33771437";

    @SneakyThrows
    public static void main(String[] args) {
        SmallTool.printMemory();

        Set<Integer> localSet = new HashSet<>(90000000);
        Set<Integer> localTaiyi = new HashSet<>(90000000);
        SmallTool.printMemory();

        DSFactory.setCurrentDSFactory(new DruidDSFactory());
        DataSource ds = DSFactory.get("server_local");
        DataSource taiyids = DSFactory.get("taiyi");
        int startPmid = 0;
        Db use = Db.use(ds);
        Db usetaiyi = Db.use(taiyids);

        String allDate = "select max(pmid) as pmid  from " + dbName;
        Entity query = use.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("pmid"));

        Integer count = query.getInt("pmid");
        int breakSize = 100000;
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 1; i <= pageSize; i++) {
            SmallTool.printMemory();
            SmallTool.printMess("当前页码+++++" + i);
            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
            SmallTool.printMess("开始id++" + start + "+++结束id++++" + end);
            if (end <= startPmid) {
                SmallTool.printMess("因为小于" + startPmid + " 略过了");
                continue;
            }
            String paramSql = "select pmid from " + dbName + " where  pmid between  " + start + " and " + end;
            List<Entity> querys = use.query(paramSql);
            for (Entity entity : querys) {
                localSet.add(entity.getInt("pmid"));
            }
        }
        SmallTool.printMess("本地数量++" + localSet.size());
        SmallTool.printMess("获取泰医");

        localTaiyi = getTaiyi(usetaiyi, localTaiyi);
        SmallTool.printMess("泰医数量++" + localTaiyi.size());

        localSet.removeAll(localTaiyi);

        SmallTool.printMess("移出后数据量++" + localSet.size());

        FileUtil.writeString(JSON.toJSONString(localSet), "/usr/loca/gcr/taiyilocalnopmid.log", "UTF-8");


    }


    @SneakyThrows
    public static Set<Integer> getTaiyi(Db use, Set<Integer> localTaiyi) {
        String allDate = "select max(pmid) as pmid  from " + taiyiTable;
        Entity query = use.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("pmid"));

        Integer count = query.getInt("pmid");
        int breakSize = 100000;
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 1; i <= pageSize; i++) {
            SmallTool.printMemory();
            SmallTool.printMess("当前页码+++++" + i);
            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
            SmallTool.printMess("开始id++" + start + "+++结束id++++" + end);

            String paramSql = "select pmid from " + taiyiTable + " where  pmid between  " + start + " and " + end;
            List<Entity> querys = use.query(paramSql);
            for (Entity entity : querys) {
                localTaiyi.add(entity.getInt("pmid"));
            }
        }
        return localTaiyi;


    }
}
